/*
 * @Author: WangLi
 * @Date: 2024-03-10 09:13:33
 * @LastEditors: WangLi
 * @LastEditTime: 2024-05-10 19:21:15
 */
import WPage from "../../common/WPage";
import { getUuid, writeBLECharacteristicValue } from "../../utils/common";

const defaultValue = {
  unlockFlag: "1",
  unlockFlagText: "开启",
  matePassword: "",
  unlockMode: "1",
  unlockModeText: "点动",
  unlockTime: "",
  unlockGapTime: "",
  unlockCount: "",
  lockMode: "1",
  lockModeText: "点动",
  lockTime: "",
  lockGapTime: "",
  lockCount: "",
  lockDelayTime: "",
  connectDistance: 6,
  unlockDistance: 80,
  lockDistance: 90,
};
WPage({
  data: {
    title: "接近开锁",
    isHighOption: false,
    unlockFlag: "1",
    unlockFlagText: "开启",
    matePassword: "",
    unlockMode: "1",
    unlockModeText: "点动",
    unlockTime: "",
    unlockGapTime: "",
    unlockCount: "",
    lockMode: "1",
    lockModeText: "点动",
    lockTime: "",
    lockGapTime: "",
    lockCount: "",
    lockDelayTime: "",
    connectDistance: 0,
    unlockDistance: 0,
    lockDistance: 0,
  },
  onLoad(options) {
    this.init();
  },
  onShow() {
    this.init();
  },
  init() {
    this.setData({ ...defaultValue });
  },
  onModeChange() {
    this.setData({ isHighOption: !this.data.isHighOption, unlockGapTime: "", unlockCount: "", lockGapTime: "", lockGapTime: "" });
  },
  onUnlockFlagChange(event) {
    const { detail } = event;
    this.setData({
      unlockFlag: detail,
      unlockFlagText: detail === "1" ? "开启" : "关闭",
    });
  },
  onUnlockModeChange(event) {
    const { detail } = event;
    this.setData({
      unlockMode: detail,
      unlockModeText: detail === "1" ? "点动" : "常通",
    });
  },
  onLockModeChange(event) {
    const { detail } = event;
    this.setData({
      lockMode: detail,
      lockModeText: detail === "1" ? "点动" : "常通",
    });
  },
  onConnectDistanceChange(event) {
    const { detail } = event;
    this.setData({
      connectDistance: detail,
    });
  },
  handleSubmit() {
    const {
      unlockFlagText,
      matePassword,
      unlockModeText,
      unlockTime,
      unlockGapTime,
      unlockCount,
      lockModeText,
      lockTime,
      lockGapTime,
      lockCount,
      lockDelayTime,
      connectDistance,
      unlockDistance,
      lockDistance,
    } = this.data;
    const serviceId = getUuid("5502");
    writeBLECharacteristicValue(serviceId, getUuid("0020"), unlockFlagText);
    writeBLECharacteristicValue(serviceId, getUuid("0021"), matePassword);
    writeBLECharacteristicValue(serviceId, getUuid("0022"), unlockModeText);
    writeBLECharacteristicValue(serviceId, getUuid("0023"), unlockTime);
    writeBLECharacteristicValue(serviceId, getUuid("002B"), unlockGapTime);
    writeBLECharacteristicValue(serviceId, getUuid("002A"), unlockCount);
    writeBLECharacteristicValue(serviceId, getUuid("0024"), lockModeText);
    writeBLECharacteristicValue(serviceId, getUuid("0025"), lockTime);
    writeBLECharacteristicValue(serviceId, getUuid("002D"), lockGapTime);
    writeBLECharacteristicValue(serviceId, getUuid("002C"), lockCount);
    writeBLECharacteristicValue(serviceId, getUuid("0026"), lockDelayTime);
    writeBLECharacteristicValue(serviceId, getUuid("0025"), connectDistance);
    writeBLECharacteristicValue(serviceId, getUuid("0025"), unlockDistance);
    writeBLECharacteristicValue(serviceId, getUuid("0025"), lockDistance);
    writeBLECharacteristicValue(serviceId, getUuid("FF20"), "", "开锁");
  },
  handleMatch() {
    const serviceId = getUuid("5502");
    writeBLECharacteristicValue(serviceId, getUuid("FF21"), "", "配对");
  },
});
